package com.lagou.cluster.controller;

import com.lagou.cluster.constants.SystemConstants;
import com.lagou.cluster.dto.LoginUserDTO;
import com.lagou.cluster.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ProjectName: S2-2
 * @Title: LoginController
 * @Package: com.lagou.cluster.controller
 * @Description: 系统登录 Controller
 * @author: Ian
 * @date: 3/19/2021 11:36 AM
 * @version: V1.0.0
 * @Copyright:
 */
@Controller
public class LoginController {
    @Autowired
    private IUserService userService;


    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    @RequestMapping("/check_user_login")
    public String login(LoginUserDTO userDTO, HttpServletRequest request) {
        boolean result = userService.checkUser(userDTO);
        if (!result) {
            return userDTO.getMessage();
        }

        request.getSession().setAttribute(SystemConstants.SESSION_ID, userDTO.getToken());
        return "redirect:/home";
    }
}
